<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('insurance_records', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 保险类型
            $table->string('insurance_type', 50); // social_insurance, housing_fund, commercial_insurance, etc.
            $table->string('insurance_category', 50)->nullable(); // pension, medical, unemployment, etc.
            $table->string('insurance_name', 100); // 具体保险名称

            // 保险方案
            $table->unsignedBigInteger('insurance_plan_id')->nullable();
            $table->string('plan_name', 100)->nullable();

            // 缴费信息
            $table->decimal('base_amount', 10, 2)->default(0); // 缴费基数
            $table->decimal('employee_contribution', 10, 2)->default(0); // 个人缴费
            $table->decimal('company_contribution', 10, 2)->default(0); // 单位缴费
            $table->decimal('total_contribution', 10, 2)->default(0); // 总缴费
            $table->string('contribution_period', 20)->default('monthly'); // monthly, annual, one_time

            // 缴费周期
            $table->date('effective_date'); // 生效日期
            $table->date('expiration_date')->nullable(); // 失效日期
            $table->string('payment_cycle', 20)->default('monthly'); // monthly, quarterly, annual

            // 状态管理
            $table->string('status', 20)->default('active'); // active, suspended, terminated
            $table->boolean('is_mandatory')->default(true); // 是否强制保险
            $table->boolean('auto_renewal')->default(true); // 是否自动续保

            // 特殊情况
            $table->boolean('is_disabled')->default(false); // 是否残疾人
            $table->string('disability_level', 20)->nullable(); // 伤残等级
            $table->text('special_notes')->nullable(); // 特殊备注

            // 关联信息
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();

            // 财务信息
            $table->string('payment_method', 50)->nullable(); // bank_transfer, cash, etc.
            $table->string('payment_reference', 100)->nullable(); // 缴费凭证号
            $table->date('last_payment_date')->nullable();
            $table->date('next_payment_date')->nullable();

            // 备注
            $table->text('notes')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'insurance_type']);
            $table->index(['insurance_type', 'status']);
            $table->index(['effective_date', 'expiration_date']);
            $table->index(['department_id']);
            $table->index(['status']);
            $table->index(['payment_cycle']);
            $table->index(['last_payment_date']);
            $table->index(['next_payment_date']);

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('insurance_plan_id')->references('id')->on('insurance_plans')->onDelete('set null');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('insurance_records');
    }
};
